import router from '@system.router';
import fetch from '@system.fetch';
import qweather from '../../common/data/qweather3ds.js';
import geolocation from '@system.geolocation';
import storage from '@system.storage';
export default {
    data: {
        title: "",
        images: [
            { src: '/common/images/bg-tv.jpg' },
            { src: '/common/images/Wallpaper.png'}
        ],
        myLocation: '161.32,39.55',
        threeDays: qweather.daily
    },
    onInit() {
        this.title = this.$t('strings.world');
        //  访问app.js中的方法
        console.log(this.$app.$def.globalData.appVersion)
        this.$app.$def.globalHello()
        const that = this
        geolocation.getLocation({
            success: function(data){
                console.log('dididi: '+JSON.stringify(data))
                that.myLocation = data.longitude+ ','+ data.latitude
            },
            complete(){
                that.get3ds()
            }
        })

    },
    // 未来三天的天气
    get3ds(){
        const that = this
        const api = 'https://devapi.qweather.com/v7/weather/3d';
        console.log('myLocation: '+this.myLocation)
        fetch.fetch({
            uri: api + '?location='+this.myLocation+'&key=590e11816b044ca98713c8caf14cd685',
            method: 'GET',
            responseType: 'json',
            success: res => {
                console.log('8888888: '+JSON.stringify(res))
                if (res.code == 200) {
                    const target = JSON.parse(res.data)
                    that.threeDays = target.daily
                    console.log(3333)
                } else {
                    console.log('查询天气失败')
                }
            }
        })
    },
    goNews(){
        router.push({
            uri: 'pages/news/news'
        })
    },
    goComp(){
        router.push({
            uri: 'pages/component/component'
        })
    },
    handleClick() {
        const animator = this.$refs.animator; // 获取ref属性为animator的DOM元素
        const state = animator.getState();
        if (state === 'paused') {
            animator.resume();
        } else if (state === 'stopped') {
            animator.start();
        } else {
            animator.pause();
        }
    },
    setValue() {
        storage.set({
            key: 'hello',
            value: {username: 'zhangsan', age: 20},
            success: function() {
                console.log('call storage.set success.');
            },
            fail: function(data, code) {
                console.log('call storage.set fail, code: ' + code + ', data: ' + data);
            },
        })
    },
    getValue() {
        storage.get({
            key: 'hello',
            success: function(data) {
                console.log('call storage.get success: ' + data);
            },
            fail: function(data, code) {
                console.log('call storage.get fail, code: ' + code + ', data: ' + data);
            },
            complete: function() {
                console.log('call complete');
            },
        });
    }
}

